﻿using DapperExtensions;
using SAICAPPWEB.BLL;
using SAICAPPWEB.Models;
using SAICAPPWEB.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAICAPPWEB.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Get(AppUser user)
        {
            var appuserbll = Factory<AppUserBll>.GetService();
            var appuser = appuserbll.Get(user.ID);
            return Json(appuser);
        }
        public ActionResult Delete(AppUser user)
        {
            Message msg = new Message();
            try { 
            var appuserbll = Factory<AppUserBll>.GetService();
             appuserbll.Delete(user.ID);
            }
            catch(Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);
        }
        public ActionResult InsertOrUpdate(AppUser user)
        {
            Message msg = new Message();
            try {
                var appuserbll = Factory<AppUserBll>.GetService();
                if (user.ID == 0)
                {
                    appuserbll.Insert(user);
                }
                else
                {
                    var appuser = appuserbll.Get(user.ID);
                    appuserbll.Update(user);
                }
               
            }
            catch(Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);
        }
        public ActionResult GetPage(PageInfo<AppUser> pageinfo)
        {
            var appuserbll = Factory<AppUserBll>.GetService();
            PageModel<AppUser> pagemodel = new PageModel<AppUser>();
            int total = 0;

            IList<IPredicate> predList = new List<IPredicate>();
            //if (!string.IsNullOrWhiteSpace(ip))
            //    predList.Add(Predicates.Field<TimeOutCompute>(p => p.IP, Operator.Like, "%" + ip + "%"));
            //if (!string.IsNullOrWhiteSpace(computername))
            //    predList.Add(Predicates.Field<TimeOutCompute>(p => p.ComputerName, Operator.Like, "%" + computername + "%"));
            //if (!string.IsNullOrWhiteSpace(username))
            //    predList.Add(Predicates.Field<TimeOutCompute>(p => p.UserName, Operator.Like, "%" + username + "%"));
            //if (!string.IsNullOrWhiteSpace(timeout))
            //    predList.Add(Predicates.Field<TimeOutCompute>(p => p.TimeOut, Operator.Ge, timeout));

            IPredicateGroup ipg = Predicates.Group(GroupOperator.And, predList.ToArray());
            pagemodel.data = appuserbll.GetPage(ipg, null, pageinfo.currentPage - 1, pageinfo.pageSize, out total);


            pagemodel.totalCount = total;
            pagemodel.currentPage = pageinfo.currentPage;
            pagemodel.pageSize = pageinfo.pageSize;
            return Json(pagemodel);
        }



    }
}
